package com.alphas.xixibackendalphas.controller;

import com.alphas.xixibackendalphas.common.BaseResponse;
import com.alphas.xixibackendalphas.common.StatusCode;
import com.alphas.xixibackendalphas.exception.BusinessException;
import com.alphas.xixibackendalphas.model.domain.User;
import com.alphas.xixibackendalphas.model.request.UserLoginRequest;
import com.alphas.xixibackendalphas.model.request.UserRegisterRequest;
import com.alphas.xixibackendalphas.service.UserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *用户接口
 */

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 用户注册
     * @param userRegisterRequest 用户注册请求体
     * @return 注册成功后返回用户id
     */
    @PostMapping ("/register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest){
        if (userRegisterRequest == null){
            throw new BusinessException(StatusCode.NULL_ERROR);
        }
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword)){
            throw new BusinessException(StatusCode.NULL_ERROR);
        }
        Long l = userService.userRegister(userAccount, userPassword, checkPassword);
        return BaseResponse.success(l);
    }

    /**
     * 用户登录
     * @param userLoginRequest  用户登录请求体
     * @param request 前端请求
     * @return 脱敏过的用户信息
     */
    @PostMapping ("/login")
    public BaseResponse<User> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request){
        if (userLoginRequest == null){
            throw new BusinessException(StatusCode.NULL_ERROR);
        }
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassword)){
            throw new BusinessException(StatusCode.NULL_ERROR);
        }
        User user = userService.userLogin(userAccount, userPassword, request);
        return BaseResponse.success(user);
    }

    /**
     * 用户注销
     * @param request 网页请求
     * @return  是否成功注销
     */
    @PostMapping("/logout")
    public BaseResponse<Integer> userLogout(HttpServletRequest request){
        userService.userLogout(request);
        return BaseResponse.success();
    }

    /**
     * 用户查找
     * @param request  前端请求
     * @param username 用户昵称
     * @return 查找后的用户列表
     */
    @GetMapping("/search")
    public BaseResponse<List<User>> userSearch(HttpServletRequest request, String username) {
        List<User> userList = userService.userSearch(request, username);
        return BaseResponse.success(userList);
    }
    /**
     * 用户推荐
     * @param request  前端请求
     * @param pageSize 分页大小
     * @param pageNum 第几页
     * @return 分页后的用户列表
     */
    @GetMapping("/recommend")
    public BaseResponse<Page<User>> userRecommend(HttpServletRequest request,int pageSize,int pageNum) {
        Page<User> userList = userService.userRecommend(request,pageSize,pageNum);
        return BaseResponse.success(userList);
    }

    /**
     * 用户删除
     * @param request 网页请求
     * @param userId 用户的id
     * @return 是否成功删除
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteUser(HttpServletRequest request, @RequestBody long userId) {
        if (userId < 0)
            throw new BusinessException(StatusCode.PARAM_ERROR);
        Boolean b = userService.deleteUser(request, userId);
        return BaseResponse.success(b);
    }

    /**
     * 获取当前用户信息
     * @param request 网页请求
     * @return 用户信息
     */
    @GetMapping("/current")
    public BaseResponse<User> getCurrentUserInfo(HttpServletRequest request) {
        User currentUserInfo = userService.getCurrentUserInfo(request);
        return BaseResponse.success(currentUserInfo);
    }

    /**
     * 根据标签匹配用户
     * @param tagNameList 标签列表
     * @return 匹配后的用户列表
     */
    @GetMapping("/search/tags")
    public BaseResponse<List<User>> searchUserByTags(@RequestParam(required = false) List<String> tagNameList){
        if(CollectionUtils.isEmpty(tagNameList)){
            throw new BusinessException(StatusCode.PARAM_ERROR);
        }
        List<User> userList = userService.searchUserByTags(tagNameList);
        return BaseResponse.success(userList);
    }

    /**
     * 修改用户信息
     * @param user 修改的用户信息
     * @param request 网络请求
     * @return 影响的用户数
     */
    @PostMapping("update")
    public BaseResponse<Integer> updateUser(@RequestBody User user,HttpServletRequest request){
        if (user == null) {
            throw new BusinessException(StatusCode.PARAM_ERROR);
        }
        if (request == null) {
            throw new BusinessException(StatusCode.PARAM_ERROR);
        }
        int result = userService.updateUser(user, request);
        return BaseResponse.success(result);
    }
    /**
     * 获取最匹配的用户
     *
     * @param num
     * @param request
     * @return
     */
    @GetMapping("/match")
    public BaseResponse<List<User>> matchUsers(long num, HttpServletRequest request) {
        if (num <= 0 || num > 20) {
            throw new BusinessException(StatusCode.PARAM_ERROR);
        }
        User user = userService.getUserLogin(request);
        return BaseResponse.success(userService.matchUsers(num, user));
    }

}
